package com.linkage.linkedlife.base

import android.os.Bundle
import android.view.View
import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.alibaba.android.arouter.launcher.ARouter
import com.gyf.immersionbar.ImmersionBar
import com.linkage.linkedlife.R
import com.linkage.linkedlife.utils.Utils

/**
 *
 * Activity基类
 * @name BaseActivity
 */
abstract class BaseActivity : AppCompatActivity() {


    /**
     * 封装的findViewByID方法
     */
    protected fun <T : View?> `$`(@IdRes id: Int): T {
        return super.findViewById<View>(id) as T
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ViewManager.getInstance().addActivity(this)
        initSystemBar()
        ARouter.getInstance().inject(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        ViewManager.getInstance().finishActivity(this)
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }

    /**
     * Setup the toolbar.
     *
     * @param toolbar   toolbar
     * @param hideTitle 是否隐藏Title
     */
    protected fun setupToolBar(toolbar: Toolbar?, hideTitle: Boolean) {
        setSupportActionBar(toolbar)
        val actionBar = supportActionBar
        if (actionBar != null) {
            actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back)
            actionBar.setDisplayHomeAsUpEnabled(true)
            actionBar.setDisplayShowHomeEnabled(true)
            if (hideTitle) {
                //隐藏Title
                actionBar.setDisplayShowTitleEnabled(false)
            }
        }
    }

    /**
     * 添加fragment
     *
     * @param fragment
     * @param frameId
     */
    fun addFragment(fragment: BaseFragment?, @IdRes frameId: Int) {
        Utils.checkNotNull(fragment)
        if (fragment != null) {
            supportFragmentManager.beginTransaction()
                .add(frameId, fragment, fragment.javaClass.simpleName)
                .addToBackStack(fragment.javaClass.simpleName)
                .commitAllowingStateLoss()
        }
    }

    /**
     * 替换fragment
     * @param fragment
     * @param frameId
     */
    fun replaceFragment(fragment: BaseFragment, @IdRes frameId: Int) {
        Utils.checkNotNull(fragment)
        supportFragmentManager.beginTransaction()
            .replace(frameId, fragment, fragment.javaClass.simpleName)
            .addToBackStack(fragment.javaClass.simpleName)
            .commitAllowingStateLoss()
    }

    /**
     * 隐藏fragment
     * @param fragment
     */
    fun hideFragment(fragment: BaseFragment) {
        Utils.checkNotNull(fragment)
        supportFragmentManager.beginTransaction()
            .hide(fragment)
            .commitAllowingStateLoss()
    }

    /**
     * 显示fragment
     * @param fragment
     */
    fun showFragment(fragment: BaseFragment) {
        Utils.checkNotNull(fragment)
        supportFragmentManager.beginTransaction()
            .show(fragment)
            .commitAllowingStateLoss()
    }

    /**
     * 移除fragment
     * @param fragment
     */
    fun removeFragment(fragment: BaseFragment) {
        Utils.checkNotNull(fragment)
        supportFragmentManager.beginTransaction()
            .remove(fragment)
            .commitAllowingStateLoss()
    }

    /**
     * 弹出栈顶部的Fragment
     */
    fun popFragment() {
        if (supportFragmentManager.backStackEntryCount > 1) {
            supportFragmentManager.popBackStack()
        } else {
            finish()
        }
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(R.anim.activity_down_in, R.anim.activity_down_out)
    }
    protected open fun initSystemBar() { //对系统状态栏的操作  需要验证不同手机型号是否实现 有没有品牌
        ImmersionBar.with(this)
            .fitsSystemWindows(true)
            .statusBarDarkFont(true, 0f)
            .statusBarColor(R.color.white).init()
    }
}